<!-- 不合格品管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_unqualified_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-button-group>
    <el-form inline :model="dataForm" label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="单号" prop="billId">
        <el-input v-model="dataForm.billId" placeholder="单号"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="商品类别">
        <el-select v-model="dataForm.ctgId" @change="changeCtg">
          <el-option v-for="item in ctgOptions" :value="item.value" :label="item.name" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName" placeholder="请选择">
          <el-button slot="append" @click="chooseSupply">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="商品品种" prop="brandName">
        <el-input v-model="dataForm.brandName" readonly="readonly">
          <el-button slot="append" @click="getBrandList">选择</el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="申报部门" prop="deptId">
        <el-select v-model="dataForm.deptId" placeholder="请选择部门" size="mini">
          <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="制单人" prop="createUserName">
        <el-input v-model="dataForm.createUserName" placeholder="制单人" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="审核人" prop="auditUserName">
        <el-input v-model="dataForm.auditUserName" placeholder="审核人" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="销售单号" prop="saleBillId">
        <el-input v-model="dataForm.saleBillId" placeholder="销售单号" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="不合格品类型">
        <el-select v-model="dataForm.handleMenthod" placeholder="不合格品类型" size="mini">
          <el-option value="1" label="报残"></el-option>
          <el-option value="0" label="退回"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="责任人">
        <el-input v-model="dataForm.dutyUserName" placeholder="责任人" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="处理状态">
        <el-select v-model="dataForm.status2" placeholder="处理状态" size="mini">
          <el-option value="3" label="未处理"></el-option>
          <el-option value="2" label="已处理"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择审核状态" size="mini">
          <el-option value="1" label="已审核"></el-option>
          <el-option value="0" label="未审核"></el-option>
        </el-select>
      </el-form-item>
      <el-row>
        <el-form-item label="是否显示不合格商品数量" label-width="165px">
          <el-checkbox v-model="dataForm.countShowFlag">显示</el-checkbox>
        </el-form-item>
        <el-form-item label="商品代码" v-if="this.dataForm.countShowFlag">
          <el-input v-model="dataForm.goodsCode"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" v-if="this.dataForm.countShowFlag">
          <el-input v-model="dataForm.viewGoodsName"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <div v-if="this.dataForm.countShowFlag">
      <el-row class="bg-row">
        退货商品数量总计：{{ this.dataForm.sum }}
      </el-row>
    </div>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="billId" label="单号" width="170"></el-table-column>
        <el-table-column prop="deptName" label="申请部门"></el-table-column>
        <el-table-column prop="status" label="处置状态" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '0'">未审核</span>
            <span v-if="scope.row.status == '1'">已审核</span>
            <span v-if="scope.row.status == '2'">已处理</span>
          </template>
        </el-table-column>
        <el-table-column prop="createUserName" label="制单人" width="80"></el-table-column>
        <el-table-column prop="createTime" label="制单日期" width="160"></el-table-column>
        <el-table-column prop="auditUserName" label="责任人" width="80"></el-table-column>
        <el-table-column prop="handleUserName" label="处理人" width="80"></el-table-column>
        <el-table-column prop="handleTime" label="处理日期" width="160"></el-table-column>
        <el-table-column label="操作" width="260" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-collection" @click="checkUnqualifiedDetail(scope.row.id)">详情</el-button>
            <el-button v-if="permissions.eims_unqualified_handle && scope.row.status=='1'" type="text" icon="el-icon-collection" @click="handleUnqualified(scope.row.id)">处理</el-button>
            <el-button v-if="permissions.eims_unqualified_handle && scope.row.status!='1'" type="text" icon="el-icon-collection" disabled>处理</el-button>
            <el-button v-if="permissions.eims_unqualified_edit && scope.row.status == '0'" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-if="permissions.eims_unqualified_del && scope.row.status == '0'" type="text"icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
            <el-button v-if="permissions.eims_unqualified_edit && scope.row.status == '1'" disabled type="text" icon="el-icon-edit">修改</el-button>
            <el-button v-if="permissions.eims_unqualified_del && scope.row.status == '1'" disabled type="text" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
    <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    <detail-dialog v-if="detailDialogVisible" ref="detailDialog"></detail-dialog>
    <supply-search v-if="supplyVisible" ref="supplySearch" @refreshData="updateSearchForm"></supply-search>
    <select-brand v-if="selectBrandVisible" ref="selectBrand" @refreshData="updateFormBrand"></select-brand>
  </div>
</template>

<script>
  import { mapGetters } from "vuex"
  import { filterParams } from "@/util/tools"
  import { fetchUnqualifiedList, delUnqualifiedObj, handleUnqualifiedObj, getUnqualifiedGoodsNumberSum } from "@/api/eims/unqualified"
  import { getDeptInfo } from "@/api/admin/dept"
  import { getCtgDataList } from "@/api/eims/check"

  import TableForm from "./form.vue"
  import DetailDialog from "./detail.vue"
  import SupplySearch from "../storage/supply-search"
  import SelectBrand from "../storage/SearchBrand"

  export default {
    data() {
      return {
        searchData: {
          key: ""
        },
        dataForm: {
          createTimeStart: "",
          createTimeEnd: "",
          billId: "",
          createUserName: "",
          deptId: "",
          auditUserName: "",
          supplyName: "",
          brandName: ""
        },
        dataList: [],
        deptOptions: [],
        ctgOptions: [],
        pageIndex: 1,
        pageSize: 200,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        detailDialogVisible: false,
        supplyVisible: false,
        selectBrandVisible: false
      };
    },
    components: {
      TableForm,
      DetailDialog,
      SupplySearch,
      SelectBrand
    },
    created() {
      this.dataForm.deptId = this.userInfo.lessDeptId
      this.getDataList()
      this.getDeptList()
      this.getGoodsCtgList()
    },
    computed: {
      ...mapGetters(["permissions","userInfo"]),
      tableHeight: function(){
        return window.innerHeight -260
      }
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 查看
      checkUnqualifiedDetail(id) {
        this.detailDialogVisible = true
        this.$nextTick(() => {
          this.$refs.detailDialog.init(id)
        });
      },
      // 删除
      deleteHandle(id) {
        this.$confirm("是否确认删除ID为" + id, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUnqualifiedObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success("删除成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 处理
      handleUnqualified(id) {
        this.$confirm("是否确认处理该不合格品单", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return handleUnqualifiedObj({id: id})
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success("处理成功")
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 搜索清空
      searchReset() {
        for (var key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      //  获取数据列表
      getDataList() {
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        this.dataListLoading = true
        fetchUnqualifiedList(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
        getUnqualifiedGoodsNumberSum(params1).then(response => {
          if(response.data.code == '0'){
            this.dataForm.sum = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      getDeptList() {
        if(this.userInfo.lessDeptId == '1'){
          getDeptInfo().then(response => {
            if(response.data.code == '0'){
              let result = response.data.data
              let list = []
              result.forEach(item => {
                list.push({
                  value: item.id,
                  label: item.name
                })
              })
              this.deptOptions = list
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }else{
          this.deptOptions = [{ value: this.userInfo.lessDeptId, label: this.userInfo.lessDeptName }]
        }
      },
      //获取商品类别
      getGoodsCtgList() {
        getCtgDataList().then(response => {
          if(response.data.code == '0'){
            let list = response.data.data.records
            let options = []
            list.forEach(item => {
              options.push({
                value: item.id,
                name: item.name
              })
            })
            this.ctgOptions = options
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      changeCtg() {
        this.dataForm.supplyId = ""
        this.dataForm.supplyName = ""
        this.dataForm.brandId = ""
        this.dataForm.brandName = ""
      },
      //获取制造商列表
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplySearch.init(this.dataForm.ctgId)
        })
      },
      updateSearchForm(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
        this.dataForm.brandId = ""
        this.dataForm.brandName = ""
      },
      // 获取商品品种信息
      getBrandList() {
        if (!this.dataForm.ctgId) {
          this.$message.warning("请选择商品类别")
          return false
        }
        if (!this.dataForm.supplyId) {
          this.$message.warning("请选择制造商")
          return false
        }
        let data = {
          ctgId: this.dataForm.ctgId,
          supplyId: this.dataForm.supplyId
        }
        this.selectBrandVisible = true
        this.$nextTick(() => {
          this.$refs.selectBrand.init(data)
        })
      },
      //  更新表单品种信息
      updateFormBrand(data) {
        this.dataForm.brandId = data.id
        this.dataForm.brandName = data.name
        this.dataForm.brandCode = data.code
        this.dataForm.name = data.name
        this.dataForm.unitType = data.unitType
      }
    }
  }
</script>
